package TestDemo.example.controller;

import TestDemo.example.pojo.Dept;
import TestDemo.example.pojo.Result;
import TestDemo.example.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;

@Slf4j
@RestController
@RequestMapping (value = "/depts")
public class DeptController {
    @Autowired
    private DeptService deptServiceImpl;

    @GetMapping
    public Result list() {
        log.info("查询全部请求数据");
        ArrayList<Dept> deptArrayList = deptServiceImpl.deptArrayList();
        return Result.success(deptArrayList);
    }

    @DeleteMapping
    public Result deleteById(@RequestParam("id") Integer id) {

        deptServiceImpl.deleteById(id);
        log.info("通过{}删除dept成功",id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept) {
        deptServiceImpl.add(dept);
        log.info("添加dept成功");
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        Dept dept = deptServiceImpl.findDeptById(id);
        log.info("通过{}查询dept成功",id);
        return Result.success(dept);
    }

    @PutMapping
    public Result updateById(@RequestBody Dept dept) {
        deptServiceImpl.updateById(dept);
        log.info("通过{}更新dept成功",dept.getId());
        return Result.success();
    }


}
